﻿using EnterpriseWebsiteApi.ActionFilters;
using EnterpriseWebsiteApi.ActionResults;
using EnterpriseWebsiteApi.Dto;
using EnterpriseWebsiteBLL;
using EnterpriseWebsiteEntity;
using EnterpriseWebsiteUtil.Helper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EnterpriseWebsiteApi.Controllers.Admin
{
    /// <summary>
    /// 联系我们(后台管理)
    /// </summary>
    [ApiController]
    [Route("api/admin/[controller]/[action]")]
    [TypeFilter(typeof(JwtAuthorizeFilter))]
    public class ContactController : ControllerBase
    {
        private readonly ContactService _contactService;
        private readonly Mapper _mapper;

        public ContactController(ContactService contactService, Mapper mapper)
        {
            _contactService = contactService;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取所有联系人
        /// </summary>
        [HttpGet]
        public ActionResult<TablePageDto<ContactDto>> GetAllContacts(int pageIndex = 0, int pageSize = 20)
        {
            TablePageDto<ContactDto> tablePageDto = new TablePageDto<ContactDto>();
            int toalNumber = 0;
            var contacts = _contactService.GetAllContacts(pageIndex, pageSize,ref toalNumber);
            var contactDtos = _mapper.MapList<ContactDto>(contacts);
            tablePageDto.ToalNumber = toalNumber;
            tablePageDto.PageIndex = pageIndex;
            tablePageDto.PageSize = pageSize;
            tablePageDto.Data = contactDtos;
            return Ok(tablePageDto);
        }

        /// <summary>
        /// 根据ID获取联系人
        /// </summary>
        [HttpGet("{id}")]
        public ActionResult<ContactDto> GetContactById(int id)
        {
            var contact = _contactService.GetContactById(id);
            if (contact == null)
            {
                return NotFound();
            }
            var contactDto = _mapper.Map<ContactDto>(contact);
            return Ok(contactDto);
        }

        /// <summary>
        /// 添加联系人
        /// </summary>
        [HttpPost]
        public ActionResult<ContactDto> AddContact([FromBody]ContactAddDto contactAddDto)
        {
            var contact = _mapper.Map<ContactEntity>(contactAddDto);
            var entity = _contactService.AddContact(contact);
            var contactDto = _mapper.Map<ContactDto>(entity);
            return CreatedAtAction(nameof(GetContactById), new { id = contactDto.Id }, contactDto);
        }

        /// <summary>
        /// 修改联系人
        /// </summary>
        [HttpPost]
        public ActionResult UpdateContact(ContactUpdateDto contactUpdateDto)
        {
            var existingContact = _contactService.GetContactById(contactUpdateDto.Id);
            if (existingContact == null)
            {
                return NotFound();
            }
            var updatedContact = _mapper.Map(contactUpdateDto, existingContact);
            _contactService.UpdateContact(updatedContact);
            return Ok(contactUpdateDto);
        }

        /// <summary>
        /// 删除联系人
        /// </summary>
        [HttpGet]
        public ActionResult DeleteContact(int id)
        {
            var existingContact = _contactService.GetContactById(id);
            if (existingContact == null)
            {
                return NotFound();
            }
            _contactService.DeleteContact(id);
            return NoContent();
        }

        /// <summary>
        /// 联系方式显示方式改变
        /// </summary>
        /// <param name="id"></param>
        /// <param name="display"></param>
        /// <param name="rightDisplay"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ContactDisplay(int id, bool display,bool rightDisplay)
        {
            try
            {
                _contactService.ContactDisplay(id,display, rightDisplay);
                return new Success();
            }
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }

        /// <summary>
        /// 改变排序
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ChangeSort(int id, int sort)
        {
            try
            {
                _contactService.ChangeSort(id, sort);
                return new Success();
            }
            catch (Exception ex)
            {

                return new Error(ex);
            }
        }
    }
}
